package org.helldorado.companybuilder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.xmlpull.v1.XmlPullParser;

public class Weapon {
	private Integer mId = 0;
	private String mTitle = "";
	private Map< Integer, List<TIPD> > mHits;
	private List<TIPD> mWeaponAbilities;
	private static Map<Integer, Weapon> sWeapons;

	private Weapon()
	{
		mHits = new HashMap<Integer, List<TIPD>>();
		mWeaponAbilities = new ArrayList<TIPD>();
		mId = 0;
		mTitle = "Weapon unknown";
	}
	
	private static Map<Integer, Weapon> getWeapons() {
		if (sWeapons == null) {
			sWeapons = getWeaponsFromFile();
		}
		return  sWeapons;
	}
	
	
	
	public static Weapon getWeaponById(Integer id) {	
		Weapon ret = getWeapons().get(id);
		if (ret == null){
			ret = new Weapon();
		}
		return ret;
	}
	
	private static List<TIPD> GetTIPDsList(String textTIPDs){
				List<TIPD> tipdList = new ArrayList<TIPD>();
				if (textTIPDs != null)
				{
					List<String> abilitiesList = Arrays.asList(textTIPDs.split(","));
					for (String ab : abilitiesList) {
						Integer abInt = Integer.parseInt(ab);
						// Find the correct Ability, State, WeaponAbility
						TIPD tipd = FindAbilityStateWeaponAbility(abInt);
						if (tipd.getId() != 0){
							tipdList.add(tipd);
						}
						
					}
				}
				return tipdList;
	}
	
	
	private static TIPD FindAbilityStateWeaponAbility(Integer id){
		TIPD tipd = null;
		tipd = WeaponAbility.getWeaponAbilityById(id);
		if (tipd.getId() != 0){
			return tipd;
		}
		tipd = State.getStateById(id);
		if (tipd.getId() != 0){
			return tipd;
		}
		tipd = Ability.getAbilityById(id);
		return tipd;
	}
	
	private static Map<Integer, Weapon> getWeaponsFromFile(){
		Map<Integer, Weapon> weapons = new HashMap<Integer, Weapon>();

		try {
			Integer resID = MainActivity.getContext().getResources().getIdentifier("hd_weapons", "xml", "org.helldorado.companybuilder");
			XmlPullParser xpp0= MainActivity.getContext().getResources().getXml(resID);
			
			int eventType0 = xpp0.getEventType();
					
			Weapon weapon = null;
			
			// Loop through pull events until we 4reach END_DOCUMENT
			while (eventType0 != XmlPullParser.END_DOCUMENT) {
				String tagname0 = xpp0.getName();

				// React to different event types appropriately
				switch (eventType0) {
				case XmlPullParser.START_TAG:
					if (tagname0.equalsIgnoreCase("weapon")) {
						weapon = new Weapon();
						
						weapon.setTitle(xpp0.getAttributeValue(null, "title"));
						weapon.setId(Integer.parseInt(xpp0.getAttributeValue(null, "id")));
					}
					if (tagname0.equalsIgnoreCase("abilities")) {
						if (weapon != null) {
							if(xpp0.next() == XmlPullParser.TEXT) {
								String str = xpp0.getText();
								weapon.mWeaponAbilities = GetTIPDsList(str);
							}
						}
					}
					if (tagname0.equalsIgnoreCase("hit1")) {
						if (weapon != null) {
							if(xpp0.next() == XmlPullParser.TEXT) {
								String str = xpp0.getText();
								weapon.mHits.put(1, GetTIPDsList(str));
							}
						}
					}
					if (tagname0.equalsIgnoreCase("hit2")) {
						if (weapon != null) {
							if(xpp0.next() == XmlPullParser.TEXT) {
								String str = xpp0.getText();
								weapon.mHits.put(2, GetTIPDsList(str));
							}
						}
					}
					if (tagname0.equalsIgnoreCase("hit3")) {
						if (weapon != null) {
							if(xpp0.next() == XmlPullParser.TEXT) {
								String str = xpp0.getText();
								weapon.mHits.put(3, GetTIPDsList(str));
							}
						}
					}
					if (tagname0.equalsIgnoreCase("hit4")) {
						if (weapon != null) {
							if(xpp0.next() == XmlPullParser.TEXT) {
								String str = xpp0.getText();
								weapon.mHits.put(4, GetTIPDsList(str));
							}
						}
					}
					if (tagname0.equalsIgnoreCase("hit5")) {
						if (weapon != null) {
							if(xpp0.next() == XmlPullParser.TEXT) {
								String str = xpp0.getText();
								weapon.mHits.put(5, GetTIPDsList(str));
							}
						}
					}
					break;
				case XmlPullParser.END_TAG:
					if (tagname0.equalsIgnoreCase("weapon")) {
						if (weapon != null){
							weapons.put(weapon.getId(), weapon);
						}
					}
					break;

				default:
					break;
				}
			
				//move on to next iteration
				eventType0 = xpp0.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return weapons;
	}

	public Integer getId() {
		return mId;
	}

	protected void setId(Integer m_id) {
		this.mId = m_id;
	}

	public List<TIPD> getWeaponAbilities() {
		return mWeaponAbilities;
	}
	
	public boolean hasHits(){
		if (mHits != null && mHits.size() > 0)
		{
			return true;
		}
		return false;
	}
	
	public List<TIPD> getHitTIPDs(Integer hit) {
		if (hasHits() == true){
			return mHits.get(hit);
		}
			
		return null;
	}

	protected String getTitle() {
		return mTitle;
	}
	
	protected void setTitle(String m_title) {
		this.mTitle = m_title;
	}
}
